import * as service from '../services/dashboard';
import { mytime } from '../utils/config';
import { message } from 'antd';

export default {
  namespace: 'dashboard',
  state: {
    uv: {
      today: 0,
      lastday: 0,
      last7day: 0,
    },
    pv: {
      today: 0,
      lastday: 0,
      last7day: 0,
    },
    sku: {
      today: 0,
      lastday: 0,
      last7day: 0,
    },
    total_money: {
      today: 0,
      lastday: 0,
      last7day: 0,
    },
    total_count: {
      today: 0,
      lastday: 0,
      last7day: 0,
    },
    new_users: {
      today: 0,
      lastday: 0,
      last7day: 0,
    },
    LineChart_pvuv: [{}, {}, {}, {}, {}, {}, {}],
    bool: true,
    key: 1,
    styles: {
      width: 500,
      height: 400,
    },
    tabel_alexa: [

      {
        title: '商品图片',
        dataIndex: 'cover_url',
        key: 'cover_url',
        render: text => <img className="img" src={`http://wxcdn.meng.im/${text}`} />,
      },
      {
        title: '商品名',
        dataIndex: 'goods_name',
        key: 'goods_name',
        render: text => <span>{text}</span>,
      },

      {
        title: '商品价格',
        dataIndex: 'purchase_price',
        key: 'purchase_price',

      },
      {

      },
      {
        title: '访问量/月',
        dataIndex: 'counts',
        key: 'counts',
      },
      {
        title: '销量/月',
        dataIndex: 'nums',
        key: 'nums',
      },
      {
        title: '访问量/天',
        dataIndex: 'counts_day',
        key: 'counts_day',
      },
      {
        title: '销量/天',
        dataIndex: 'nums_day',
        key: 'nums_day',
      },
    ],
    tabel_map_month: [
      {
        title: '地区',
        dataIndex: 'region',
        key: 'region',
      },
      {
        title: '激活用户',
        dataIndex: 'nums',
        key: 'nums',
      },
      {
        title: '活跃用户',
        dataIndex: 'count',
        key: 'count',
      },
    ],
    tabel_map_day: [
      {
        title: '地区',
        dataIndex: 'region',
        key: 'region',
      },
      {
        title: '访问量/天',
        dataIndex: 'nums',
        key: 'nums',
      },
    ],
    order_log: [],
    searchPUS: null,
    LineChartOrder: null,
    LineChartMoney: null,
  },
  reducers: {
    save(state, { payload: data }) {
      let bool;
      if (data.bool) {
        bool = true;
      } else {
        bool = false;
      }

      return { ...state, ...data, bool };
    },


  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if ((pathname === '/') || (pathname === '/Dashboard')) {
          dispatch({ type: 'fetch', payload: query });
          dispatch({ type: 'searchOrders' });
          dispatch({ type: 'searchMoney' });
        }
      });
    },
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const { data } = yield call(service.fetch);
      yield put({ type: 'save', payload: data });
    },
    *searchPUS({ payload }, { call, put }) {
      const { keys } = payload;
      const { data } = yield call(service.linePUS, payload);
      console.log('searchPUS');
      console.log(data);
      if (data && data.success) {
        const arr = data.data;
        const datas = analysis(arr, keys);
        if (data && data.success) {
          yield put({
            type: 'save',
            payload: {
              searchPUS: datas,
            },
          });
        } else {
          message.error(data.msg);
        }
      }
    },
    *searchOrders({ payload }, { call, put }) {
      const { data } = yield call(service.lineOrders);
      const arr = data.data;

      const datas = analysis(arr, ['xm_orders', 'gd_orders']);
      if (data && data.success) {
        yield put({
          type: 'save',
          payload: {
            LineChartOrder: datas,
          },
        });
      } else {
        message.error(data.msg);
      }
    },
    *searchMoney({ payload }, { call, put }) {
      const { data } = yield call(service.lineMoney);
      const arr = data.data;
      const datas = analysis(arr, ['xm_money', 'gd_money']);
      console.log('datas');
      console.log(datas);
      if (data && data.success) {
        yield put({
          type: 'save',
          payload: {
            LineChartMoney: datas,
          },
        });
      } else {
        message.error(data.msg);
      }
    },
  },

};


function analysis(arr, keys) {
  const newArr = [];

  for (let i = 0; i < arr[0][keys[0]].length; i++) {
    let obj = {};
    for (let j = 0; j < keys.length; j++) {
      obj = Object.assign(obj, arr[j][keys[j]][i]);
    }
    newArr.push(obj);
  }
  if (keys[0] === 'xm_money') {
    for (let i = 0; i < newArr.length; i++) {
      newArr[i].xm_money = newArr[i].xm_money / 100;
      newArr[i].gd_money = newArr[i].gd_money / 100;
    }
    console.log(newArr);
  }

  return newArr;
}
